package cn.geodata.dataview.action;


import java.io.IOException;
import java.lang.reflect.Array;
import java.util.logging.Logger;

import org.apache.commons.collections.CollectionUtils;

import cn.geodata.dataview.util.Util;

public class Repository {
	private static Logger log = Logger.getAnonymousLogger();
	
	private String path;
	private int id;
	
	public String list() throws Exception {
		return "success";
	}

	public String add() throws Exception {
		if(path == null){
			throw new NullPointerException("Require path");
		}
		String[] _paths = Util.getRepositoryPaths();
		for(String _p : _paths){
			if(_p.equals(path)){
				return "success";
			}
		}
		
		Util.getConfigure().put(Util.REPOSITORY_PROPERTY_NAME, Util.combinesArray(_paths));
		Util.storeConfig();

		return "success";
	}
	
	public String delete() throws Exception {
		if(id < 0){
			throw new NullPointerException("Require id");
		}

		String[] _paths = Util.getRepositoryPaths();
		Util.getConfigure().remove(Util.REPOSITORY_PROPERTY_NAME);
		for(int i=0;i< _paths.length;i++){
			if(i != id){
				Util.getConfigure().put(Util.REPOSITORY_PROPERTY_NAME, _paths[i]);
			}
		}
		Util.storeConfig();
		
		return "success";
	}
	
	public void setPath(String path){
		this.path = path;
	}
	
	public String[] getPaths() throws IOException{
		String[] _paths = Util.getRepositoryPaths();
		log.info("Path count:" + _paths.length);
		
		return _paths;
	}
	
	public void setId(int id){
		this.id = id;
	}
}
